package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.common.db_service.ConfigManager;
import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Hfdb;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("getDomainBlack2DBService")
public class GetDomainBlack2DBService {
    ResultSet rs = null;
    BaseDao baseDao = null;
    String sql = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public boolean get(String path,String MessageType,String policy_instance,GeneralFileProcess pro){

        boolean result = false;
        String name = ConfigManager.getInstance().getString("domBlackFileName");

        if (path != null) {
            List<String> list = pro.getContent(path, 1);
            if (!list.isEmpty()) {
                init();
                sql = "select * from Hfdb";
                Object[] param={};
                rs=baseDao.executeSQL(sql,param);
                Map<String, Integer> map = new HashMap<String, Integer>();
                try {
                    if (rs.next()) {
                        while (rs.next()){
                            map.put(rs.getString("Filter"),rs.getInt("Id"));
                        }
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                Hfdb hfdb = null;
                try {
                    for (String line : list) {
                        String[] ss = line.split("\t");
                        String url = ss[0];
                        if (!map.containsKey(url)) {
                            Object[] params={url};
//                            hfdb = new Hfdb(url);
                            sql = "insert into hfdb (filtel ) values (?)";
                            baseDao.executeUpdate(sql, param);
                            int id = map.size()+1;
                            map.put(url, id);
                        }
                    }
                    result = true;
                } catch (Exception e) {
                    result = false;
                } finally {
                   close();
                }
                try {
                    PolicySupport policySupport = PolicySupport.getInstance();
                    String policy = policySupport.getApi().getMessage("0xCF", "默认库", 0).getSvnFile().getContent();
                    String comment = policySupport.getApi().getMessage("0xCF", "默认库", 0).getSvnFile().getComment();
                    policySupport.getApi().updatePolicy("0xCF", "默认库", 0, policy, comment);
                } catch (Exception e) {
                }

            }
        }
        return result;
    }
//    boolean result = false;
//    String name = ConfigManager.getInstance().getString("domBlackFileName");
//
//    if (path != null) {
//        List<String> list = pro.getContent(path, 1);
//        if (!list.isEmpty()) {
//            SessionFactory sf = HibernateUtil.getSessionFactory();
//            Session session = sf.openSession();
//            session.clear();
//            Transaction tx = session.beginTransaction();
//            String hql = "from Hfdb";
//            List hfdbs = session.createQuery(hql).list();
//            Map<String, Integer> map = new HashMap<String, Integer>();
//            if (!hfdbs.isEmpty()) {
//                for (int i = 0; i < hfdbs.size(); i++) {
//                    Hfdb hd = (Hfdb) hfdbs.get(i);
//                    map.put(hd.getFilter(), hd.getId());
//                }
//            }
//            Hfdb hfdb = null;
//            try {
//                for (String line : list) {
//                    String[] ss = line.split("\t");
//                    String url = ss[0];
//                    if (!map.containsKey(url)) {
//                        hfdb = new Hfdb(url);
//                        session.save(hfdb);
//                        int id = map.size()+1;
//                        map.put(url, id);
//                    }
//                }
//                tx.commit();
//                result = true;
//            } catch (Exception e) {
//                tx.rollback();
//                result = false;
//            } finally {
//                session.close();
//            }
//            try {
//                PolicySupport policySupport = PolicySupport.getInstance();
//                String policy = policySupport.getApi().getMessage("0xCF", "默认库", 0).getSvnFile().getContent();
//                String comment = policySupport.getApi().getMessage("0xCF", "默认库", 0).getSvnFile().getComment();
//                policySupport.getApi().updatePolicy("0xCF", "默认库", 0, policy, comment);
//            } catch (Exception e) {
//            }
//
//        }
//    }
//    return result;
//}
}
